package com.gk.panda.apis.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.dahuatech.icc.exception.ClientException;
import com.gk.panda.apis.Vo.RealtimeShootListVo;
import com.gk.panda.apis.dto.RealtimeShootFileDto;
import com.gk.panda.apis.dto.RealtimeShootListDto;
import com.gk.panda.apis.dto.RealtimeShootSaveDto;
import com.gk.panda.apis.log.Log;
import com.gk.panda.apis.service.FdfsService;
import com.gk.panda.apis.service.RealtimeShootService;
import com.gk.panda.commons.result.Result;
import com.gk.panda.pojo.RealtimeShoot;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.io.UnsupportedEncodingException;
import java.util.Arrays;
import java.util.List;

@RestController
@RequestMapping("/edmp/realTimeShoot")
@Api(tags = "实时抓拍")
public class RealtimeShootController {

    @Autowired
    private RealtimeShootService realtimeShootService;

    @Autowired
    private FdfsService fdfsService;

    @ApiOperation("上传实时抓拍")
    @Log
    @PostMapping("/add")
    public Result<String> addRealTimeShoot(@RequestBody RealtimeShootSaveDto dto) {
        boolean save = realtimeShootService.addRealTimeShoot(dto);
        if (save) return Result.write(200,"上传成功");
        return Result.write(201,"上传失败");
    }

    @ApiOperation("获取实时抓拍列表")
    @PostMapping("/page")
    public Result<Page<RealtimeShootListVo>> getRealTimeShootPage(@RequestBody RealtimeShootListDto dto) throws ClientException {
        return Result.ok(realtimeShootService.getRealTimeShootPage(dto));
    }

    @ApiOperation("查看实时抓拍详情")
    @GetMapping("/getInfo/{id}")
    public Result<List<RealtimeShoot>> getTransectDetails(@PathVariable("id") String id) {
        String[] ids = id.split(",");
        return Result.ok(realtimeShootService.listByIds(Arrays.asList(ids)));
    }

    @ApiOperation("根据id删除实时抓拍图片视频")
    @Log
    @DeleteMapping("/{id}")
    public Result<String> removeById(@PathVariable("id") String id) {
        return realtimeShootService.delById(id);
    }

    @ApiOperation("根据id获取图片视频地址")
    @GetMapping("/getPicPathById")
    public Result<List<RealtimeShoot>> getPicPathById(String id) {
        return Result.ok(realtimeShootService.getPicPathById(id));
    }

    @ApiOperation("归档")
    @Log
    @PostMapping("/filePicAndVideo")
    public Result<String> filePicAndVideo(@RequestBody RealtimeShootFileDto dto) throws UnsupportedEncodingException {
        int result = realtimeShootService.filePicAndVideo(dto);
        if (result == 0) return Result.ok("归档成功");
        return Result.error("归档失败");
    }

    @ApiOperation("暂不归档")
    @Log
    @GetMapping("/withholdFile")
    public Result<String> withholdFile(String realtimeShootIds) {
        realtimeShootService.withholdFile(realtimeShootIds);
        return Result.ok("操作成功");
    }
}
